﻿Imports System.Security.Cryptography
Imports System.Text.Encoding

Module Alice
    Public progress As Integer = 0
    Public Function alice_ETH_mac(ByVal SSID As String, ByVal num_magic() As String, ByVal MAC As String) As String
        Dim hex_SSID As String
        Dim alice_MAC As String = ""
        Dim Pirelli_Prefix() As String = {"002553", "001D8B", "002233", "00238E", "001CA2", "0017C2", "38229D"}
        alice_MAC = ""
        If MAC <> "" Then
            MAC = MAC.ToUpper
            For Padding As Short = 0 To 2
                If Padding > 0 Then
                    hex_SSID = Hex(CStr(Padding) & SSID)
                Else
                    hex_SSID = Hex(SSID)
                End If
                If hex_SSID.Chars(0) = MAC.Chars(5) Then
                    alice_MAC = MAC & hex_SSID.Substring(1, 6)
                End If
            Next
        Else
            For Padding As Short = 0 To 2
                If Padding > 0 Then
                    hex_SSID = Hex(CStr(Padding) & SSID)
                Else
                    hex_SSID = Hex(SSID)
                End If
                For Each Pirel_Prefix As String In Pirelli_Prefix
                    If hex_SSID.Chars(0) = Pirel_Prefix.Chars(5) Then
                        For Each Str As String In num_magic
                            If SSID.Substring(0, 3) = Str.Substring(0, 3) Then
                                'Dim a As String = Str.Substring(22, 6)
                                If Str.Substring(22, 6) = Pirel_Prefix Then
                                    alice_MAC = Pirel_Prefix & hex_SSID.Substring(1, 6)
                                    Exit For
                                End If
                            End If
                        Next
                    End If
                Next
            Next
        End If

        Return alice_MAC
    End Function

    Public Function Alice_Seriale(ByVal SSID As String, ByVal num_magic() As String, ByVal tSN1 As String, ByVal tK As Byte, ByVal tQ As Integer) As String
        Dim SN2 As String = ""
        Dim serial As String = ""
        serial = ""
        If tSN1 <> "" And tK <> 0 And tQ <> 0 Then
            If (Val(SSID) - tQ) Mod tK Then
                serial = "K e/o Q errati"
            Else
                SN2 = CStr((Val(SSID) - tQ) / tK).PadLeft(7, "0") 'Calcolo del seriale (SSID - Q)/K, aggiungo a sinistra tanti 0 quanti ne servono per arrivare a 7 cifre
                serial = CStr(tSN1 & "X" & SN2)
            End If
        Else
            For Each Str As String In num_magic
                If Str.Substring(0, 3) = SSID.Substring(0, 3) Then
                    Dim Q As Int32 = Str.Substring(13, 8)
                    Dim K As Byte = Str.Substring(10, 2)
                    Dim SN1 As Int32 = Str.Substring(4, 5)
                    If (Val(SSID) - Q) Mod K Then
                        serial = "K e/o Q errati"
                        Exit For
                    Else
                        SN2 = CStr((Val(SSID) - Q) / K).PadLeft(7, "0") 'Calcolo del seriale (SSID - Q)/K, aggiungo a sinistra tanti 0 quanti ne servono per arrivare a 7 cifre
                        serial = CStr(SN1 & "X" & SN2)
                    End If
                End If
            Next
        End If
        Return serial
    End Function
    Public Function Alice_Agpf_WPA(ByVal alice_MAC As String, ByVal alice_serial As String) As String
        Dim SHA_Manager As New SHA256Managed()
        Dim SHA_24_Bytes(23) As Byte
        Dim alice_WPA As String = ""
        Const charSET As String = "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123"
        Dim Fixed_String() As Byte = {100, 198, 221, 227, 229, 121, 182, 217, 134, 150, 141, 52, 69, 210, 59, 21, 202, 175, 18, 132, 2, 172, 86, 0, 5, 206, 32, 117, 145, 63, 220, 232} 'Stringa fissa gia convertita in un array di Byte
        If alice_serial <> "" Then
            Array.Copy((SHA_Manager.ComputeHash(Fixed_String.Concat(ASCII.GetBytes(alice_serial).Concat(HexStringToByte(alice_MAC))).ToArray)), SHA_24_Bytes, 24) 'Concatenazione dei 3 array di bytes derivanti dalla stinga fissa, seriali e mac, calcolo dello sha e estrapolazione dei primi 24 bytes della stringa sha ricevute in output
            For Each b As Byte In SHA_24_Bytes
                alice_WPA &= charSET.Chars(b) 'In base al byte preso in considerazione prendiamo il carattere con indice corrispondente dal charSET
            Next
        End If
        Return alice_WPA
    End Function
    Public Function brute_serial(ByVal WPA As String, ByVal MAC As String, ByVal SN1 As String) As String
        Dim twpa As String = ""
        For i As Integer = 1 To 1000000
            twpa = Alice_Agpf_WPA(MAC, SN1 & "X0" & CStr(i).PadLeft(6, "0"))
            If twpa = LCase(WPA) Then
                'i = 0
                Return SN1 & "X0" & CStr(progress).PadLeft(6, "0")
            ElseIf i = 1000000 Then
                'i = 0
                Return "Nessun SN trovato"
            End If
        Next
    End Function
    Public Function HexStringToByte(ByVal HEX_Str As String) As Byte()
        Try
            Dim byte_Res(HEX_Str.Length / 2 - 1) As Byte
            For i As Byte = 1 To byte_Res.GetUpperBound(0)
                byte_Res(i) = CByte("&h" & Mid(HEX_Str, (i * 2) + 1, 2))
            Next
            Return byte_Res
        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            ProjectData.ClearProjectError()
        End Try
    End Function
End Module